#import "AllChatsView.h"
#import "SolarSeekApplication.h"
#import "ChatsController.h"
#import "ChatElements.h"

@implementation AllChatsView
- (void)awakeFromNib {
	[NSApp addObserver:self forKeyPath:@"engine.chatsController.chatsInside.values" options:NSKeyValueObservingOptionOld context:nil];
	_element = [[ChatsElement alloc] initWithView:self expandable:YES];
	[nibTableRooms setTarget:self];
	[nibTableRooms setDoubleAction:@selector(joinRoom:)];
	
	[nibTableUsers setTarget:self];
	[nibTableUsers setDoubleAction:@selector(startPrivateChats:)];
}

- (void)dealloc { 
	[_element release];
	[NSApp removeObserver:self forKeyPath:@"engine.chatsController.chatsInside.values"];
	[super dealloc];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	[_element refreshAndRequestAttention:YES];
	[NSApp updateSourceList];
	if([[change objectForKey:NSKeyValueChangeKindKey] intValue] == NSKeyValueChangeInsertion && [[[[NSApp engine] chatsController] chatsInside] count])
		[NSApp performSelector:@selector(expandSourceListElement:) withObject:_element afterDelay:0.0];
//	else if([[change objectForKey:NSKeyValueChangeKindKey] intValue]==NSKeyValueChangeRemoval)
//		[nibControllerConversations removeObjects:[change objectForKey:NSKeyValueChangeOldKey]];
}

- (void)addChatDidEnd:(NSWindow *)sheet returnCode:(int)code contextInfo:(void *)info {
	[sheet orderOut:self];
	if(code == NSOKButton) {
		if([nibMenuTypeChat selectedTag]==2) {
			NSString *person = [nibTextAddChat stringValue];
			PrivateChat *chat = [[[NSApp engine] chatsController] chatWithPeer:[[NSApp engine] peerNamed:person]];
			[NSApp expandSourceListElement:_element];
			[NSApp selectSourceListElement:[ChatsElement elementForChat:chat]];
		}
		else {
			NSString *roomname = [nibTextAddChat stringValue];
			Room *room = [[[NSApp engine] chatsController] roomNamed:roomname];
			[[[NSApp engine] chatsController] addRoomToList:room];
			RoomChat *chat = [[[NSApp engine] chatsController] chatWithRoom:room autojoin:YES];
			[NSApp expandSourceListElement:_element];
			[NSApp selectSourceListElement:_element];
		}
	}
}

- (IBAction)endSheetNo:(id)sender {
	[NSApp endSheet:[sender window] returnCode:NSCancelButton];
}

- (IBAction)endSheetYes:(id)sender { [NSApp endSheet:[sender window] returnCode:NSOKButton]; }

- (IBAction)addChat:(id)sender {
	[NSApp beginSheet:nibPanelAddChat modalForWindow:[NSApp keyWindow] modalDelegate:self didEndSelector:@selector(addChatDidEnd:returnCode:contextInfo:) contextInfo:nil];
}

- (IBAction)addRoom:(id)sender
{
	[nibMenuTypeChat selectItemWithTag:1];
	[NSApp beginSheet:nibPanelAddChat modalForWindow:[NSApp keyWindow] modalDelegate:self didEndSelector:@selector(addChatDidEnd:returnCode:contextInfo:) contextInfo:nil];
}


- (IBAction)addPrivateChat:(id)sender {
	[nibMenuTypeChat selectItemWithTag:2];
	[NSApp beginSheet:nibPanelAddChat modalForWindow:[NSApp keyWindow] modalDelegate:self didEndSelector:@selector(addChatDidEnd:returnCode:contextInfo:) contextInfo:nil];
}

- (void)addPrivateChatDidEnd:(NSWindow *)sheet returnCode:(int)code contextInfo:(void *)info {
	[sheet orderOut:self];
}

@end
